package com.yq.homework.day20;

import com.sun.org.apache.regexp.internal.REUtil;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/20 19:50
 *
 */
public class Demo03 {
    public static void main(String[] args) throws CloneNotSupportedException {
        //    测试
        Teacher t1 = new Teacher(32, "杨老师", new Student(23, "李明", new Star(12, "小明")));

        Teacher t2 = t1.clone();
        t1.stu.setName("11111");

        System.out.println("修改后的t1的student的name："+t1.stu.getName());
        System.out.println("t2的student的name："+t2.stu.getName());

    }
}

class Teacher implements Cloneable {
    private int age;
    private String name;
    Student stu;

    public Teacher(int age, String name, Student stu) {
        this.age = age;
        this.name = name;
        this.stu = stu;
    }

    @Override
    protected Teacher clone() throws CloneNotSupportedException {
        Teacher cloneTeacher = (Teacher) super.clone();
        Student cloneStudent = cloneTeacher.stu.clone();
        cloneTeacher.stu = cloneStudent;
        return cloneTeacher;
    }
}

class Student implements Cloneable {
    private int age;
    private String name;
    Star s;

    public Student(int age, String name, Star s) {
        this.age = age;
        this.name = name;
        this.s = s;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    protected Student clone() throws CloneNotSupportedException {
        Student cloneStudent = (Student) super.clone();
        Star cloneStar = cloneStudent.s.clone();
        cloneStudent.s = cloneStar;
        return cloneStudent;
    }
}

class Star implements Cloneable {
    private int age;
    private String name;

    public Star(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    protected Star clone() throws CloneNotSupportedException {
        return (Star) super.clone();
    }
}

